using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Abp.Domain.Repositories;
using Abp.Collections.Extensions;
using System.Linq.Expressions;

namespace Yozeev.BusinessLogic.DomainService
{
	/// <summary>
	/// TicketDetail领域层的业务管理
	///</summary>
	public class TicketDetailManager :YozeevDomainServiceBase, ITicketDetailManager
    {
		
		private readonly IRepository<TicketDetail,Guid> _repository;

        private readonly IRepository<ActivityDetail, Guid> _activityDetailRepository;
        private readonly IRepository<Activity, Guid> _activityRepository;
        private readonly IRepository<Customer, Guid> _customerRepository;
        private readonly IRepository<Schedule, Guid> _scheduleRepository;
        private readonly IRepository<ActivityTemp, Guid> _activityTempRepository;

        /// <summary>
        /// TicketDetail的构造方法
        ///</summary>
        public TicketDetailManager(
			IRepository<TicketDetail, Guid> repository,
			IRepository<ActivityDetail, Guid> activityDetailRepository
            , IRepository<Activity, Guid> activityRepository
            , IRepository<Customer, Guid> customerRepository
            , IRepository<Schedule, Guid> scheduleRepository
            , IRepository<ActivityTemp, Guid> activityTempRepository
            )
		{
			_repository =  repository;
            _activityDetailRepository = activityDetailRepository;
            _activityRepository = activityRepository;
            _customerRepository = customerRepository;
            _scheduleRepository = scheduleRepository;
            _activityTempRepository = activityTempRepository;
        }

		/// <summary>
		/// 初始化
		///</summary>
		public void InitTicketDetail()
		{
			throw new NotImplementedException();
		}

        public async Task<List<TicketDetail>> GeTicketDetailByActivityId(string activityId)
        {
            var td = await _repository.GetAllListAsync();
            return td.Where(t => t.ActivityId == Guid.Parse(activityId)).ToList();
        }

		/// <summary>
		/// 根据ticket id查询游船信息
		/// </summary>
		/// <param name="ticketId"></param>
		/// <returns></returns>
		public Boat GetBoatByTicketId(string ticketId)
		{
			var ticket = _repository.FirstOrDefault(t => t.Id.ToString() == ticketId);
			return ticket.Boat;
		}
        public List<TicketDetail> GetTicketDetailList(List<Guid> guids)
        {
            var guidArrary = guids.ToArray();
            //var list = _repository.GetAll();
            var list = from ticketDetail in _repository.GetAll().Where(m=> guids.Contains(m.Id))
                       select new { ticketDetail,ticketDetail.Schedule,ticketDetail.ActivityDetail,ticketDetail.ActivityDetail.Activity,ticketDetail.Customer }
                       ;
            var ticketDetailList = new List<TicketDetail>();
            list.ToList().ForEach(t => {
                var ticketDetail = t.ticketDetail;
                ticketDetail.Schedule = t.Schedule;
                ticketDetail.ActivityDetail = t.ActivityDetail;
                ticketDetail.ActivityDetail.Activity = t.Activity;
                ticketDetail.Customer = ticketDetail.Customer;
                ticketDetailList.Add(ticketDetail);
            });
            return ticketDetailList;
        }
        /// <summary>
        /// 根据二维码获取票据信息
        /// </summary>
        /// <param name="QRCode"></param>
        /// <returns></returns>
        public List<TicketDetail> GetTicketDetailByQRCode(string QRCode,string CardNo)
        {
            Expression<Func<TicketDetail, bool>> expression = entity => 1 == 0;
            if (!string.IsNullOrWhiteSpace(QRCode))
                expression = entity => entity.QRCode == QRCode;
            if (!string.IsNullOrWhiteSpace(CardNo))
                expression = entity => entity.Customer.CertificatesNum == CardNo;
            var query = from entity in _repository.GetAll().Where(expression)
                        select new
                        {
                            entity,
                            entity.Schedule,
                            entity.ActivityDetail,
                            entity.ActivityDetail.TicketPrice,
                            entity.Customer,
                            entity.Route,
                            entity.Boat,
                            entity.ActivityDetail.Activity,
                            entity.ActivityDetail.TicketPrice.Ticket,
                            entity.Branch,
                            entity.CreatorUser
                        };
            var list = new List<TicketDetail>();
            query.ToList().ForEach(temp => {
                var _entity = temp.entity;
                _entity.Schedule = temp.Schedule;
                _entity.ActivityDetail = temp.ActivityDetail;
                _entity.ActivityDetail.TicketPrice = temp.TicketPrice;
                _entity.Customer = temp.Customer;
                _entity.Schedule.Route = temp.Route;
                _entity.Schedule.Boat = temp.Boat;
                _entity.ActivityDetail.Activity = temp.Activity;
                _entity.ActivityDetail.TicketPrice.Ticket = temp.Ticket;
                list.Add(_entity);
            });
            return list.OrderBy(m=>m.CreationTime).ToList();
        }
        /// <summary>
        /// 更新票据
        /// </summary>
        /// <param name="ticketDetail"></param>
        /// <returns></returns>
        public async Task UpdateTicketDetail(TicketDetail ticketDetail)
        {
            await _repository.UpdateAsync(ticketDetail);
        }

        public List<TicketDetail> GetTicketDetailByTemp(Guid activityTempId)
        {
            var list = from ticketDetail in _repository.GetAll()
                       join activityTemp in _activityTempRepository.GetAll().Where(m => m.Id == activityTempId) on ticketDetail.ActivityId equals activityTemp.ActivityId
                       select ticketDetail;
            return list.ToList();
        }

        public async Task<TicketDetail> GetTicketDetailById(Guid ticketDetailId)
        {
            return await _repository.GetAsync(ticketDetailId);
        }
        /// <summary>
        /// 根据航班查询该旅行社购票数
        /// </summary>
        /// <param name="TravelId"></param>
        /// <param name="ScheduleId"></param>
        /// <returns></returns>
        public async Task<int> GetCountByTravel(Guid TravelId,Guid ScheduleId)
        {
            var count = await _repository.CountAsync(m => m.ActivityDetail.Activity.TravelAgencyId == TravelId && m.ScheduleId == ScheduleId);
            return count;
        }
    }
}
